use clap::Parser;
mod model;
use model::*;
use std::{
  fs::File,
  io::{Read, Write},
  path::Path,
};

#[derive(Debug, Parser)]
#[clap(author, version, about, long_about = None)]
struct Args {
  #[clap(value_parser)]
  input: String,
}

fn generate(p: &Path) -> Result<(), anyhow::Error> {
  let mut f = File::open(p)?;
  let p = p.canonicalize()?;
  let directory = p.parent().unwrap();
  let mut buf = String::new();
  f.read_to_string(&mut buf)?;
  let model: Model = serde_json::from_str(&buf)?;
  let mut template = String::from("import { createIconSet } from 'react-native-vector-icons';\n\n");
  template.push_str("const glyphMap = {\n");
  model.glyphs.into_iter().for_each(|glyphs| {
    template.push_str(&format!("  '{}': {},\n", glyphs.name, glyphs.unicode_decimal));
  });
  template.push_str("};\n\n");
  template.push_str("export const IconFont = createIconSet(glyphMap, 'iconfont', 'iconfont.ttf');\n");
  let mut new_file = File::create(format!("{}/iconfont.js", directory.to_str().unwrap()))?;
  new_file.write_all(template.as_bytes())?;
  Ok(())
}

fn main() -> Result<(), anyhow::Error> {
  let args: Args = Args::parse();
  let p = Path::new(&args.input);
  generate(p)
}
